/******************************************************************************
** 
**	File Name: udebug.c
**	
**	Description:	RTOS Unit Tests. Debug logging services.
**
**					This source file consists of services required for 
**					unit test automation.
**
**	Services:
**
**		void	utest_log		( unsigned char SequenceNum )
**		void	utst_counter	( unsigned short count )
**
**  Notes:			Unit test automation! Check breakpoints settings before
**					executing the test or import breakpoint definition file
**					../RTOS/UTEST/udebug.xml
**
** Change history:
**	
** 03/10/2012 - AK - File created. 
**
******************************************************************************/

#include "rtos.h"
#include "udebug.h"

static RTOS_PID_t *		RunID;	
static RTOS_PID_t *		TopID;

static RTOS_KDATA_t const * 	RTOS_KDATA = &RtosKernelData;

static unsigned char	SysLv;

static unsigned char	LogCnt1 = 0;
static unsigned char	LogCnt2 = 0;

#ifndef MAX_UTEST_COUNT
#define MAX_UTEST_COUNT 32
#endif

/******************************************************************************
** Function:		void utest_log(unsigned char SequenceNum)
** Description:        
** Parameters:
** Returns:
** Notes:                                                              
******************************************************************************/
void utest_log(unsigned char SequenceNum) 
{
	
	LogCnt1 = ( SequenceNum == 0) ? 0 : (LogCnt1+1); 
	
	SysLv = RTOS_KDATA->level;
	
	TopID = RTOS_KDATA->queue;
	RunID = RTOS_KDATA->running;		
	
	if ( SequenceNum != LogCnt1 ) {
		rtos_halt_os();
	}	
	
	if (++LogCnt2 == MAX_UTEST_COUNT) {
		rtos_halt_os();		
	} 
}

/******************************************************************************
** Function:		void utst_counter( unsigned short count )
** Description:        
** Parameters:
** Returns:
** Notes:                                                              
******************************************************************************/
void utst_counter( unsigned short count ) 
{
	if (LogCnt1++ >= count) 
	{
		asm volatile ("break\t\n");
	}
}
		